/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataConverters.DataFormatters.formattersimpl;

import DataConverters.DataFormatters.AbstractDataFormatter;
import DataConverters.exceptions.DataConvertException;
import DataConverters.interfaces.IConvertable;
import DataConverters.interfaces.IConvertableFile;
import DataConverters.interfaces.dataformulaobject.intefaces.BooleanCell;
import DataConverters.interfaces.dataformulaobject.intefaces.BooleanFormulaCell;
import DataConverters.interfaces.dataformulaobject.intefaces.Cell;
import DataConverters.interfaces.dataformulaobject.intefaces.CellType;
import DataConverters.interfaces.dataformulaobject.intefaces.DateCell;
import DataConverters.interfaces.dataformulaobject.intefaces.DateFormulaCell;
import DataConverters.interfaces.dataformulaobject.intefaces.NumberCell;
import DataConverters.interfaces.dataformulaobject.intefaces.NumberFormulaCell;
import DataConverters.interfaces.dataformulaobject.intefaces.StringFormulaCell;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.biff.formula.FormulaException;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author Nobody
 */
public class Excel7Formatter extends AbstractDataFormatter {

    public Excel7Formatter(SortedSet<Cell> list) {
        super(list);
    }

    public Excel7Formatter() {
        super(new TreeSet<Cell>());
    }

    @Override
    public void format(IConvertable source) throws DataConvertException {
        IConvertableFile fileSource = (IConvertableFile) source;
        FileOutputStream stream = null;
        try {
            stream = new FileOutputStream((fileSource.asFile()).getAbsolutePath());
            XSSFWorkbook workBook = new XSSFWorkbook();
            XSSFSheet sheet = workBook.createSheet("Sheet 1");
            for (Cell cell : list) {
                int j = cell.getColumn();
                int i = cell.getRow();
                if (sheet.getRow(j) == null) {
                    sheet.createRow(j);
                }
                XSSFCell writeCell = sheet.getRow(j).createCell(i);
                if (cell.getType().equals(CellType.BOOLEAN)) {
                    BooleanCell booleanCell = (BooleanCell) cell;
                    writeCell.setCellValue(booleanCell.getValue());
                } else if (cell.getType().equals(CellType.BOOLEAN_FORMULA)) {
                    BooleanFormulaCell booleanFormulaCell = (BooleanFormulaCell) cell;
                    try {
                        writeCell.setCellFormula(booleanFormulaCell.getFormula());
                    } catch (FormulaException ex) {
                        writeCell.setCellErrorValue(FormulaError.REF);
                    }
                } else if (cell.getType().equals(CellType.DATE)) {
                    DateCell dateCell = (DateCell) cell;
                    writeCell.setCellValue(dateCell.getDate());
                } else if (cell.getType().equals(CellType.DATE_FORMULA)) {
                    DateFormulaCell dateFormulaCell = (DateFormulaCell) cell;
                    try {
                        writeCell.setCellFormula(dateFormulaCell.getFormula());
                    } catch (FormulaException ex) {
                        writeCell.setCellErrorValue(FormulaError.REF);
                    }
                } else if (cell.getType().equals(CellType.ERROR)) {
                    writeCell.setCellErrorValue(FormulaError.NA); //TODO handle various error code
                } else if (cell.getType().equals(CellType.FORMULA_ERROR)) {
                    writeCell.setCellErrorValue(FormulaError.REF);
                } else if (cell.getType().equals(CellType.LABEL)) {
                    writeCell.setCellValue(cell.getContents());
                } else if (cell.getType().equals(CellType.NUMBER)) {
                    NumberCell numberCell = (NumberCell) cell;
                    writeCell.setCellValue(numberCell.getValue());
                } else if (cell.getType().equals(CellType.NUMBER_FORMULA)) {
                    NumberFormulaCell numberFormulaCell = (NumberFormulaCell) cell;
                    writeCell.setCellValue(numberFormulaCell.getValue());
                    try {
                        writeCell.setCellFormula(numberFormulaCell.getFormula().replace("=", ""));
                    } catch (FormulaException ex) {
                        writeCell.setCellErrorValue(FormulaError.REF); //TODO handle various error code
                    }
                } else if (cell.getType().equals(CellType.STRING_FORMULA)) {
                    StringFormulaCell stringFormulaCell = (StringFormulaCell) cell;
                    try {
                        writeCell.setCellValue(stringFormulaCell.getFormula());
                        writeCell.setCellFormula(stringFormulaCell.getFormula().replace("=",""));
                    } catch (FormulaException ex) {
                        writeCell.setCellErrorValue(FormulaError.REF); //TODO handle various error code
                    }
                }
            }
            workBook.write(stream);
            stream.close();
        } catch (FileNotFoundException ex) {
            throw new DataConvertException("", ex);
        } catch (IOException ex) {
            throw new DataConvertException("", ex);
        } finally {
            try {
                stream.close();
            } catch (IOException ex) {
                throw new DataConvertException("", ex);
            }
        }

    }
}
